
//
// 130424 - AH - Created.
// 130609 - AH - Introduced working directory.
//

package org.japril.swing.jutil.xmloperations;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import org.japril.jutil.IOUtil;
import org.japril.jutil.xmloperations.XmlOperation;
import org.japril.jutil.xmloperations.XmlOperations;
import org.japril.swing.ExceptionDialog;

/**
 * <p>
 *   Manager of 
 *   {@link XmlOperation}s.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class XmlOperationsManager extends JFrame 
{
  private final File m_wsdir;
  
  private final XmlOperationsPanel m_op=new XmlOperationsPanel();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param wsdir Workspace directory.
   * @param ops   {@link XmlOperation}s to manage.
   */
  private XmlOperationsManager(File wsdir, XmlOperations ops)
  {
    if (wsdir==null)
    {
      throw new NullPointerException("wsdir");
    }
    if (!wsdir.isDirectory())
    {
      throw new IllegalArgumentException(wsdir+" not a directory");
    }
    
    m_wsdir=wsdir;
    
    if (ops==null)
    {
      throw new NullPointerException("ops");
    }
    
    setTitle("XML Operation Toy");
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    JPanel north=new JPanel(new FlowLayout(FlowLayout.LEFT));
    north.add(makeToolBar());
    c.add(north, BorderLayout.NORTH);
    
    if (ops!=null) m_op.setOperations(ops);
    c.add(m_op, BorderLayout.CENTER);
    
    pack();
    setSize(800, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  public JToolBar makeToolBar()
  {
    JToolBar bar=new JToolBar();
    
    bar.add(m_save);
    bar.add(m_xml);
    bar.add(m_op.m_createOperation);
    bar.add(m_op.m_removeOperation);
    bar.add(m_op.m_desc);
    bar.add(m_report);
    bar.add(m_code);
    bar.add(m_op.getPathsPanel().m_create);
    bar.add(m_op.getPathsPanel().m_remove);
    
    return bar;
  }
  
  private final Action m_code=new AbstractAction("Code")
  {
    public void actionPerformed(ActionEvent e)
    {
      JFileChooser chooser=new JFileChooser();
      chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      chooser.setDialogTitle("Source dir.");
      chooser.setCurrentDirectory(m_wsdir);
      if (chooser.showOpenDialog(XmlOperationsManager.this)==
        JFileChooser.APPROVE_OPTION)
      {
        try
        {
          File dir=chooser.getSelectedFile();
          
          XmlOperationsToJavaCode code=new XmlOperationsToJavaCode(dir);
          
          XmlOperations ops=m_op.getOperations();
          int n=ops.size();
          for (int i=0; i<n; i++)
          {
            XmlOperation curr=ops.getOperation(i);
            code.generateXmlOperation(curr);
          }
          
          code.generateXmlOperationsFactory(ops);
        }
        catch (Exception x)
        {
          ExceptionDialog.showErrorDialog(x);
        }
      }
    }
  };
  
  private final Action m_save=new AbstractAction("Save")
  {
    public void actionPerformed(ActionEvent e)
    {
      try
      {
        File f=new File(m_wsdir, "./xmlops.txt");
        XmlOperations ops=m_op.getOperations();
        IOUtil.save(f, ops.toString());
        
        String descs=ops.docToString();
        descs=(descs==null) ? "" : descs.trim();
        if (descs.length()>0)
        {
          f=new File(m_wsdir, "./xmldesc.txt");
          IOUtil.save(f, descs);
        }
      }
      catch (IOException iox)
      {
        ExceptionDialog.showErrorDialog(iox);
      }
    }
  };
  
  private final Action m_report=new AbstractAction("Report")
  {
    public void actionPerformed(ActionEvent e)
    {
      XmlOperations ops=m_op.getOperations();
      XmlOperationsReportCompiler comp=
        new PlainTextXmlOperationsReportCompiler();
      String report=comp.compile(ops);
      
      JFileChooser chooser=new JFileChooser();
      chooser.setCurrentDirectory(m_wsdir);
      if (chooser.showSaveDialog(XmlOperationsManager.this)==
       JFileChooser.APPROVE_OPTION)
      {
        File f=chooser.getSelectedFile();
        try
        {
          IOUtil.save(f, report);
        }
        catch (IOException iox)
        {
          ExceptionDialog.showErrorDialog(iox);
        }
      }
    }
  };
  
  private JFileChooser m_chooser=null;
  
  private final Action m_xml=new AbstractAction("XML")
  {
    public void actionPerformed(ActionEvent e)
    {
      if (m_chooser==null)
      {
        m_chooser=new JFileChooser();
        m_chooser.setDialogTitle("XML File");
        m_chooser.setCurrentDirectory(m_wsdir);
      }
      
      if (m_chooser.showOpenDialog(XmlOperationsManager.this)==
        JFileChooser.APPROVE_OPTION)
      {
        File f=m_chooser.getSelectedFile();
        try
        {
          XmlOperations ops=m_op.getOperations();
          String xml=IOUtil.read(f);
          ops.perform(xml);
        }
        catch (IOException iox)
        {
          ExceptionDialog.showErrorDialog(iox);
        }
      }
      
    }
  };
  
  public static void main(String[] args)
  {
    File wsdir=new File("./");
    JFileChooser chooser=new JFileChooser();
    chooser.setDialogTitle("XOperations workspace");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setCurrentDirectory(wsdir);
    if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
    {
      wsdir=chooser.getSelectedFile();
    }
    
    try
    {
      XmlOperations ops=new XmlOperations();
      
      File f=new File(wsdir, "./xmlops.txt");
      if (f.exists())
      {
        ops=new XmlOperations(f);
      }
      
      f=new File(wsdir, "./xmldesc.txt");
      if (f.exists())
      {
        String txt=IOUtil.read(f);
        Map<String, String> descs=XmlOperations.parseDesc(txt);
        ops.setDescriptions(descs);
      }
      
      final XmlOperationsManager m=new XmlOperationsManager(wsdir, ops);
      SwingUtilities.invokeLater
      (
        new Runnable() 
        {
          public void run(){ m.setVisible(true); }
        }
      );
    }
    catch (IOException iox)
    {
      ExceptionDialog.showErrorDialog(iox);
      System.exit(-1);
    }
  }
  
}
